package com.arong.biBackend.manager;

import com.arong.biBackend.exception.BusinessException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class GuavaRetryManager {

    @Bean
    public Retryer defaultRetryer() {
        Retryer<Boolean> defaultRetryer = RetryerBuilder.<Boolean>newBuilder()
                // retryIf 重试条件
                .retryIfException()
                .retryIfExceptionOfType(BusinessException.class)
                // 等待策略
                .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
                // 停止策略
                .withStopStrategy(StopStrategies.stopAfterAttempt(4))
                .build();

        return defaultRetryer;
    }
}
